package com.epfresh.supplier.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.api.widget.NoScrollListView;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.StandingBookListEntity;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by shenjie on 2017/9/20.
 */

public class StandingBookAdapter extends BaseAdapter {

    private List<StandingBookListEntity.StandingBookList> lists;
    private Context context;

    public StandingBookAdapter(List<StandingBookListEntity.StandingBookList> lists, Context context) {
        this.lists = lists;
        this.context = context;
    }

    @Override
    public int getCount() {
        if(lists==null){
            return 0;
        }
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        if(lists==null){
            return null;
        }
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d("sssss",position+"");
        ViewHolder holder=null;
        if(convertView==null){
            holder=new ViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.standingbook_item_list,null);
            convertView.setTag(holder);
            holder.menuId= (TextView) convertView.findViewById(R.id.menuId);
            holder.date= (TextView) convertView.findViewById(R.id.date);
            holder.customerInfo= (TextView) convertView.findViewById(R.id.customerinfo);
            holder.gathering= (TextView) convertView.findViewById(R.id.gathering);
            holder.wait= (TextView) convertView.findViewById(R.id.wait);
            holder.onaccount= (TextView) convertView.findViewById(R.id.onaccount);
            holder.image= (ImageView) convertView.findViewById(R.id.image);
        }
        if(((NoScrollListView)parent).isOnMeasure()){
            return convertView;
        }
        StandingBookListEntity.StandingBookList entity=lists.get(position);
        holder= (ViewHolder) convertView.getTag();
        holder.menuId.setText(entity.getOrderNo()+"");
        String createOrderTime=entity.getCreateOrderTime();
        String[] order=createOrderTime.split(" ");
        String[] date=order[0].split("-");
        String[] time=order[1].split(":");
        holder.date.setText(date[1]+"-"+date[2]+" "+time[0]+":"+time[1]);
        holder.customerInfo.setText(entity.getCustomerName()+"  "+entity.getBuyerPhone());
        holder.gathering.setText("收款:"+" "+entity.getPayFee()+"元");
        holder.wait.setText("应收:"+" "+entity.getReceivableFee()+"元");
        if(entity.getDebtFee()>0){
            holder.onaccount.setVisibility(View.VISIBLE);
            holder.onaccount.setText("欠款:"+" "+entity.getDebtFee()+"元");
            holder.onaccount.setTextColor(context.getResources().getColor(R.color.red));
            holder.image.setImageResource(R.mipmap.unclear);
        }else if(entity.getRefundedFee()!=0){
            holder.onaccount.setVisibility(View.VISIBLE);
//            String xx=new BigDecimal(entity.getReceivableFee()+"").subtract(new BigDecimal(entity.getPayFee()+"")).doubleValue()+"";
            holder.onaccount.setText("退款（现金）: -"+entity.getRefundedFee()+"元");
            holder.onaccount.setTextColor(context.getResources().getColor(R.color.font_color_def));
            holder.image.setImageResource(R.mipmap.clear);
        }else {
            holder.onaccount.setVisibility(View.INVISIBLE);
            holder.image.setImageResource(R.mipmap.clear);
        }




        return convertView;
    }

    class ViewHolder{
        TextView menuId,date,customerInfo,gathering,wait,onaccount;
        ImageView image;
    }

    public List<StandingBookListEntity.StandingBookList> getLists() {
        return lists;
    }

    public void setLists(List<StandingBookListEntity.StandingBookList> lists) {
        this.lists = lists;
    }
}
